import Vue from 'vue'
import VueRouter from 'vue-router'
import App from '../App'

const layout = r => require.ensure([], () => r(require('../page/layout')), 'layout')
const demo = r => require.ensure([], () => r(require('../page/demo')), 'demo')

const routes = [{
  path: '/',
  component: App,
  children: [{
    path: '',
    redirect: '/layout'
  }, {
    path: '/layout',
    component: layout,
    meta: { keepAlive: true }
  }, {
    path: '/demo',
    component: demo,
    meta: { keepAlive: true }
  }]
}]

Vue.use(VueRouter)
export default new VueRouter({
  routes,
  mode: 'hash',
  strict: process.env.NODE_ENV !== 'production',
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      if (from.meta.keepAlive) {
        from.meta.savedPosition = document.body.scrollTop
      }
      return {
        x: 0,
        y: to.meta.savedPosition || 0
      }
    }
  }
})
